package com.zqwh.xtool.api.offline;

import android.content.Context;

import com.zqwh.xtool.greendao.DaoManager;
import com.zqwh.xtool.greendao.LendOrderDetailDao;
import com.zqwh.xtool.greendao.db.LendOrderDetail;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class LendOrderDetailResposity {
    private DaoManager daoManager;
    private LendOrderDetailDao lendOrderDetailDao;

    public LendOrderDetailResposity(Context context){
        daoManager = DaoManager.getInstance();
        daoManager.init(context);
        lendOrderDetailDao = daoManager.getDaoSession().getLendOrderDetailDao();
    }

    /**
     * 插入记录
     *
     * @param lendOrderDetail
     * @return
     */
    public boolean insertLendOrderDetail(LendOrderDetail lendOrderDetail) {
        return lendOrderDetailDao.insertOrReplace(lendOrderDetail) != -1;
    }

    /**
     * 插入多条记录
     * @param lendOrderDetailList
     * @return
     */
    public boolean insertMultLendOrderDetail(final List<LendOrderDetail> lendOrderDetailList){
        boolean flag = false;
        try {
            daoManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (LendOrderDetail lendOrderDetail : lendOrderDetailList) {
                        lendOrderDetailDao.insertOrReplace(lendOrderDetail);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询
     * @param userId
     * @param devId
     * @param rentStatus
     * @param stockIdList
     * @return
     */
    public List<LendOrderDetail> queryLendOrderDetailList(Long userId, Long devId, String rentStatus, List<String> stockIdList){
        QueryBuilder<LendOrderDetail> qb = lendOrderDetailDao.queryBuilder();
        qb.where(LendOrderDetailDao.Properties.Valid.eq(true),
                LendOrderDetailDao.Properties.UserId.eq(userId),
                LendOrderDetailDao.Properties.DevId.eq(devId),
                LendOrderDetailDao.Properties.RentStatus.eq(rentStatus),
                LendOrderDetailDao.Properties.StockId.in(stockIdList));
        return qb.list();
    }

    /**
     * 查询
     * @param userId
     * @param devId
     * @param rentStatus
     * @param hasRFID
     * @return
     */
    public List<LendOrderDetail> queryLendOrderDetailList(Long userId, Long devId, String rentStatus, boolean hasRFID){
        QueryBuilder<LendOrderDetail> qb = lendOrderDetailDao.queryBuilder();
        qb.where(LendOrderDetailDao.Properties.Valid.eq(true),
                LendOrderDetailDao.Properties.UserId.eq(userId),
                LendOrderDetailDao.Properties.DevId.eq(devId),
                LendOrderDetailDao.Properties.RentStatus.eq(rentStatus),
                LendOrderDetailDao.Properties.HasRFID.eq(hasRFID));
        return qb.list();
    }

    /**
     * 查询更新过的数据
     * @return
     */
    public List<LendOrderDetail> queryLendOrderDetailUpdated(){
        QueryBuilder<LendOrderDetail> qb = lendOrderDetailDao.queryBuilder();
        qb.where(LendOrderDetailDao.Properties.Updated.eq(true));
        return qb.list();
    }


    /**
     * 删除所有数据
     */
    public void deleteAll(){
        lendOrderDetailDao.deleteAll();
    }
}
